package cn.wolfcode.luowowo.acticle.service.impl;


import cn.wolfcode.luowowo.acticle.domain.QuestionAndAnswer;
import cn.wolfcode.luowowo.acticle.domain.QuestionAndAnswerContent;
import cn.wolfcode.luowowo.acticle.mapper.QuestionAndAnswerCommentMapper;
import cn.wolfcode.luowowo.acticle.mapper.QuestionAndAnswerContentMapper;
import cn.wolfcode.luowowo.acticle.mapper.QuestionAndAnswerMapper;
import cn.wolfcode.luowowo.acticle.query.QuestionAndAnswerQuery;
import cn.wolfcode.luowowo.acticle.service.IQuestionAndAnswerService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
@Service
@Transactional
public class QuestionAndAnswerServiceImpl implements IQuestionAndAnswerService {
    @Autowired
    private QuestionAndAnswerMapper questionAndAnswerMapper;

    @Autowired
    private QuestionAndAnswerContentMapper questionAndAnswerContentMapper;

    @Autowired
    private QuestionAndAnswerCommentMapper questionAndAnswerCommentMapper;





    @Override
    public PageInfo query(QuestionAndAnswerQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());

        return new PageInfo(questionAndAnswerMapper.selectForList(qo));
    }
    @Override
    public PageInfo query2(QuestionAndAnswerQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());

        return new PageInfo(questionAndAnswerMapper.selectForList(qo));
    }

    @Override
    public void changeState(QuestionAndAnswer questionAndAnswer) {
        //如果是发布状态，设置
        questionAndAnswerMapper.updateQuestionAndAnswerState(questionAndAnswer);
    }

    @Override
    public QuestionAndAnswer get(Long id) {
        QuestionAndAnswer questionAndAnswer = questionAndAnswerMapper.selectByPrimaryKey(id);
        QuestionAndAnswerContent questionAndAnswerContent = questionAndAnswerContentMapper.selectByPrimaryKey(id);
        questionAndAnswer.setQuestionAndAnswerContent(questionAndAnswerContent);
        return questionAndAnswer;
    }

    @Override
    public List<QuestionAndAnswer> queryQuestionAndAnswerByDestId(Long destId) {


        QuestionAndAnswerQuery qo = new QuestionAndAnswerQuery();
        qo.setDestId(destId);
        PageHelper.startPage(qo.getCurrentPage(), 3, "qaa.viewnum desc");
        return questionAndAnswerMapper.selectForList(qo);
    }

    @Override
    public Long saveOrUpdate(QuestionAndAnswer questionAndAnswer) {
        //作者id

        //游记摘要
        if(questionAndAnswer.getQuestionAndAnswerContent().getContent().length() < 200){
            questionAndAnswer.setSummary(questionAndAnswer.getQuestionAndAnswerContent().getContent());
        }else{
            String summary = questionAndAnswer.getQuestionAndAnswerContent().getContent().substring(0, 200);
            questionAndAnswer.setSummary(summary);
        }


        //保存
        if(questionAndAnswer.getId() == null){
            questionAndAnswer.setQuestionTime(new Date());
            questionAndAnswerMapper.insert(questionAndAnswer);
            QuestionAndAnswerContent content = questionAndAnswer.getQuestionAndAnswerContent();
            content.setId(questionAndAnswer.getId());
            questionAndAnswerContentMapper.insert(content);
        }else{

            questionAndAnswerMapper.updateByPrimaryKey(questionAndAnswer);
            QuestionAndAnswerContent content = questionAndAnswer.getQuestionAndAnswerContent();
            content.setId(questionAndAnswer.getId());
            questionAndAnswerContentMapper.updateByPrimaryKey(content);

        }

        return questionAndAnswer.getId();
    }

    @Override
    public List<QuestionAndAnswer> list() {
        return questionAndAnswerMapper.selectAll();
    }


}
